package com.str.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author ZhangWu, Anything is possible!
 * @date 2022/3/17 14:04
 * @description aes加密
 */
@Slf4j
public class AesUtils {
    /**
     * 加密方式
     */
    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";

    /**
     * aes加密
     * @param content 待加密内容
     * @param key 加密秘钥
     * @return
     */
    public static String encrypt(String content, String key) {
        try {
            // 获得密码的字节数组
            byte[] raw = key.getBytes();
            // 根据密码生成AES密钥
            SecretKeySpec sKey = new SecretKeySpec(raw, "AES");
            // 根据指定算法ALGORITHM自成密码器
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            // 初始化密码器，第一个参数为加密(ENCRYPT_MODE)或者解密(DECRYPT_MODE)操作，第二个参数为生成的AES密钥
            cipher.init(Cipher.ENCRYPT_MODE, sKey);
            // 获取加密内容的字节数组(设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
            byte [] byteContent = content.getBytes("utf-8");
            // 密码器加密数据
            byte [] encodeContent = cipher.doFinal(byteContent);
            // 将加密后的数据转换为字符串返回
            return Base64.encodeBase64String(encodeContent);
        } catch (Exception e) {
            log.error(content+"，秘钥："+key+"，进行aes加密Exception",e);
            return null;
        }
    }

    /**
     * 解密
     * @param encryptStr 解密字符串
     * @param decryptKey 加密秘钥
     * @return
     */
    public static String decrypt(String encryptStr, String decryptKey) {
        try {
            byte[] encryptBytes = new BASE64Decoder().decodeBuffer(encryptStr);
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128);
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
            byte[] decryptBytes = cipher.doFinal(encryptBytes);
            return new String(decryptBytes);
        } catch (Exception e) {
            log.error(encryptStr+"，秘钥："+decryptKey+"，进行aes解密Exception",e);
            return null;
        }
    }

    public static String loginPwdKey(String userId){
        int len = 16;
        if(userId.length() > len){
            return userId.substring(0,16);
        }
        StringBuffer pwdKey = new StringBuffer(userId);
        for(int i=0;i< len -userId.length();i++){
            pwdKey.append("0");
        }
        return pwdKey.toString();
    }

    public static void main(String[] args) {
        String userId = "32719489d50248b6a618a03d66d0ce22";
        String pwdKey = AesUtils.loginPwdKey(userId);
        System.out.println(pwdKey);
       // String password = AesUtils.encrypt("a12345678","11abe1ad69e24f94");
      String pwd = AesUtils.decrypt("gdDYeU/4173Ck7i2IlM2Bg==","2ed89f15d6d34a89");
        //System.out.println("加密后："+password);
        System.out.println("解密后："+pwd);
    }
}
